#!/bin/bash

basefun="`which functions.sh`"
FILTER_XLS="`which mainsoft-filter.py`"
PARSE_XLS="`which get-mainsoft-rev.py`"
PARSE_INI="`which parseini.sh`"
SVN_OPTION="--no-auth-cache --non-interactive"
tempRawSh="/tmp/rawMS.sh"
mergeCfgFile="`which project-svn.ini`"
SVNCMD=`which svn.shc`
if [ "$PARSE_XLS" == "" ] || [ "$FILTER_XLS" == "" ] ; then
	echo "Can't find get-mainsoft-rev.py or mainsoft-filter.py, exit..."
	exit
fi

function InitVars()
{
	allProject=(`$PARSE_INI "field" "$mergeCfgFile" "CONFIGS" "allProjects"` )
	selProject="`getChoice "请选择待处理的ini文件section:" allProject 1`"

	repoRootURL="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "repoRootURL"`"
	productPath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "productPath"`"
	trunkPath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "trunkPath"`"
	driverPath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "driverPath"`"
	modemPath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "modemPath"`"
	appRepoRootURL="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "appRepoRootURL"`"
	appPackagePath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "appPackagePath"`"
	appproductPath="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "appproductPath"`"
	framework_trunk_start_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "framework_trunk_start_version"`"
	app_trunk_start_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "app_trunk_start_version"`"
	framework_trunk_to_product_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "framework_trunk_to_product_version"`"
	app_trunk_to_product_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "app_trunk_to_product_version"`"
	framework_product_start_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "framework_product_start_version"`"
	app_product_start_version="`$PARSE_INI "field" "$mergeCfgFile" "$selProject" "app_product_start_version"`"
	
}

function get_svn_version_from_file()
{
	local file=$1
	echo "file=$file"
}

#过滤掉产品线打分支前的版本
function filter_creat_frame_branch_version()
{
	local all_frame_version=$1
	local new_frame_version=
	all_frame_version="`echo $all_frame_version | tr "," " "`"
	for i in $all_frame_version;do
		if [ $i -gt $framework_trunk_to_product_version ];then
			new_frame_version="$new_frame_version $i"
		fi
	done
	new_frame_version="`echo $new_frame_version | tr " " ","`"
	echo "$new_frame_version"
}
function filter_creat_app_branch_version()
{
	local all_app_version=$1
	local new_app_version=
	all_app_version="`echo $all_app_version | tr "," " "`"
	for i in $all_app_version;do
		
		if [ $i -gt $app_trunk_to_product_version ];then
			new_app_version="$new_app_version $i"
		fi
	done
	new_app_version="`echo $new_app_version | tr " " ","`"
	echo "$new_app_version"
}
#过滤掉产品线打分支前的版本

#过滤掉产品线已经合并过的版本
function filter_product_merged_version()
{
	local merge_version=$1
	local merged_version=$2
	local process_version=
	merge_version="`echo $merge_version | tr "," " "`"
	for i in $merge_version;do
		if [[ ! "$merged_version" =~ "$i" ]];then
			process_version="$process_version $i"
		fi
	done
	process_version="`echo $process_version | tr " " ","`"
	echo "$process_version"
}
#过滤掉产品线已经合并过的版本

function GetSvnLogPath()
{
	local ropoRoot="$1"
	local svnRev="$2"
	local specPath="$3"
	local result=""
	echo "logloglogloglogloglogloglogloglogloglogloglogloglog"
	echo "$SVNCMD log $ropoRoot -r $svnRev -v $SVN_OPTION | grep \"$specPath\""
	result="`$SVNCMD log $ropoRoot -r $svnRev -v $SVN_OPTION | grep "$specPath"`"

	echo "endendendendendendendendendendendendendendendendendend"
	echo "$result"
}

function process_log_file()
{
	local svn_url=$1
	local svn_version_range=$2
	local project_name=$3
	all_svn_version="`sh_get_all_svn_log_info_by_project $svn_version_range $svn_url $project_name`"
	echo "`SortUniqNumber "$all_svn_version"`"
}

function SortUniqNumber()
{
	local origNum="$1"
	echo $origNum | sed "s/\s/ /g" | tr " " "\n" | sort -n | uniq | tr "\n" ',' 
}


#提供在svn产品线上某个范围中同步过的版本号
function sh_get_all_svn_log_info()
{
	local url=$1
	local serch_keyword=$2
	local all_svn_version=
	local result_version=
	if [ -z "$serch_keyword" ];then
		all_svn_version="`$SVNCMD log -v $url --stop-on-copy | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`"
	else
		all_svn_version="`$SVNCMD log -v $url --stop-on-copy | grep -F "$serch_keyword" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`"
	fi
	result_version=`echo $all_svn_version |tr -s " " "\n" | sort -n | tr -s "\n" " "`
	echo $result_version | tr " " ","
}


#按项目名提供在svn上某个范围中同步过的版本号,项目名称必须处于行的开头
function sh_get_all_svn_log_info_by_project()
{
	local svn_version_range=$1
	local url=$2
	local serch_keyword=$3
	local all_svn_version=
	local result_version=
	if [ -z "$serch_keyword" ];then
		all_svn_version="`$SVNCMD log -v -r $svn_version_range $url | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`"
	else
		all_svn_version="`$SVNCMD log -v -r $svn_version_range $url | grep "^$serch_keyword" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`"
	fi
	result_version=`echo $all_svn_version |tr -s " " "\n" | sort -n | tr -s "\n" " "`
	echo $result_version | tr " " ","
}
#提供给测试的提取svn记录
function sh_get_log_info_by_keyword()
{
	local url=$1
	local svn_version_range=$2
	local serch_keyword=$3
	$SVNCMD log -r $svn_version_range $url | grep -B 3 -A 1 "$serch_keyword"
}
